package com.wenan.ui.popup
{
	import com.amcharts.geom.Rect;
	import com.wenan.ui.floating.MultiDimensionalNavigationDialog;
	import com.wenan.ui.floating.ShopViewDialog;
	
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import mx.core.FlexGlobals;
	import mx.core.UIComponent;
	import mx.managers.PopUpManager;
	
	public class PopupMenuManager
	{
		static private var applicationListenered:Boolean = false;
		
		static private var popupControl:UIComponent;
		static private var child:UIComponent;
		
		static public function popupAtControl(popupControl:UIComponent, referenceControl:Object, displayObject:DisplayObject = null):void
		{
			var popupPoint:Point = referenceControl.parent.localToGlobal(
				new Point(referenceControl.x, referenceControl.y + referenceControl.height));
			
			popupAt(popupControl, popupPoint.x, popupPoint.y, displayObject);
		}
		
		static public function popupAtControlMoving(popupControl:UIComponent,moveX:Number, referenceControl:Object, displayObject:DisplayObject = null):void
		{
			var popupPoint:Point = referenceControl.parent.localToGlobal(
				new Point(referenceControl.x, referenceControl.y + referenceControl.height));
			
			popupAt(popupControl, popupPoint.x-moveX, popupPoint.y, displayObject);
		}
		
		static public function popupAt(popupControl:UIComponent, x:Number, y:Number, displayObject:DisplayObject):void
		{
			if (!(popupControl is ShopViewDialog))
			{
				removePopupControl();
			}
			var application:Object = FlexGlobals.topLevelApplication;
			if (displayObject)
			{
				displayObject.addEventListener(MouseEvent.MOUSE_DOWN, function(event:MouseEvent):void {
					PopUpManager.removePopUp(popupControl);
				});
				if (x + popupControl.width > application.width)
				{
					x = application.width - popupControl.width;
				}
				if (y + popupControl.height > application.height)
				{
					y = application.height - popupControl.height;
				}
				
				popupControl.visible = false;
				
				var myFilters:Array = new Array();
				myFilters.push(new DropShadowFilter(2, 45));
				popupControl.filters = myFilters;
				
				popupControl.x = x;
				popupControl.y = y;
				popupControl.visible = true;
				
				PopUpManager.addPopUp(popupControl, displayObject);
				return;
			}
			
			
			if (application)
			{
				if (!applicationListenered)
				{
					application.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
					applicationListenered = true;							
				}
				
				if (x + popupControl.width > application.width)
				{
					x = application.width - popupControl.width;
				}
				if (y + popupControl.height > application.height)
				{
					y = application.height - popupControl.height;
				}
				
				popupControl.visible = false;
				
				var myFilters:Array = new Array();
				myFilters.push(new DropShadowFilter(2, 45));
				popupControl.filters = myFilters;
				
				popupControl.x = x;
				popupControl.y = y;
				popupControl.visible = true;
				
				application.addElement(popupControl);
				if (popupControl is ShopViewDialog)
				{
					child = popupControl;
				} else 
				{
					PopupMenuManager.popupControl = popupControl;	
				}
			}
		}
		
		static public function removePopupControl(isChild:Boolean = false):void
		{
			if (popupControl)
			{
				var application:Object = FlexGlobals.topLevelApplication;
				if (isChild)		//点击孩子的关闭，只移除孩子
				{
					if (popupControl is MultiDimensionalNavigationDialog)
					{
						if (child)
						{
							application.removeElement(child);
							child.filters = null;
							child.visible = false;
							child = null;
						}
					}
					return;
				} else {		//点击父亲的关闭，将移除孩子和父亲
					if (popupControl is MultiDimensionalNavigationDialog)
					{
						if (child)
						{
							application.removeElement(child);
							child.filters = null;
							child.visible = false;
							child = null;
						}
					}
				}
				application.removeElement(popupControl);
				
				popupControl.filters = null;
				popupControl.visible = false;
				popupControl = null;
			}
		}
		
		/**
		 * 鼠标在应用程序任意位置点击。
		 * */
		static private function mouseDownHandler(event:MouseEvent) : void
		{
			if (popupControl)
			{
				var rect:Rectangle = getPopupControlBounds();
				var application:Object = FlexGlobals.topLevelApplication;
				if (!rect.containsPoint(new Point(application.mouseX, application.mouseY))) 
				{
					if (popupControl is MultiDimensionalNavigationDialog)
					{
						var rect1:Rectangle = getChildBounds();
						if (rect1)
						{
							if (!rect1.containsPoint(new Point(application.mouseX, application.mouseY)))
							{
								if (child)
								{
									application.removeElement(child);
									child.filters = null;
									child.visible = false;
									child = null;
								}
							}else 
							{
								return;
							}
						}
					}
					application.removeElement(popupControl);
					popupControl.filters = null;
					popupControl.visible = false;
					popupControl = null;
				}
			}
		}
		
		static private function getPopupControlBounds():Rectangle
		{
			var point:Point = popupControl.parent.localToGlobal(
				new Point(popupControl.x, popupControl.y));
			return new Rectangle(point.x, point.y, popupControl.width, popupControl.height);
		}
		
		static private function getChildBounds():Rectangle
		{
			if (child)
			{
				var point:Point = child.parent.localToGlobal(
					new Point(child.x, child.y));
				return new Rectangle(point.x, point.y, child.width, child.height);
			}
			return null;
		}
	}
}